<template>
    <d2-container>
        <el-form :inline="true">
            <el-button
                    type="primary"
                    style="float:right;margin:10px;"
                    icon="el-icon-plus"
                    @click="newPosition"
            >新建职位
            </el-button>
        </el-form>

        <el-table :data="dataList" border style="width: 100%">
            <el-table-column prop="name" label="职位名称"></el-table-column>

            <el-table-column fixed="right" label="操作">
                <template slot-scope="scope">
                        <el-button @click="editClick(scope.row)" type="text" size="small">修改</el-button>
                    <el-button @click="deleteClick(scope.row)" type="text" size="small">删除</el-button>

                </template>
            </el-table-column>
        </el-table>


        <el-pagination
                background
                layout="total,sizes,prev, pager, next,jumper"
                :page-size="pageSize"
                :current-page="currentPage"
                :total="total"
                @current-change="handleCurrentChange"
                @size-change="handlerSizeChange"
                @next-click="handleNextPage"
                @prev-click="handlePrevClick"
        ></el-pagination>
    </d2-container>
</template>

<script>
    import request from "@/plugin/axios";
        import {mapState} from "vuex";

    export default {
        data() {
            return {
                dataList: [],
                total: 0,
                pageSize: 10,
                currentPage: 1,
                classroom: [],
                dataSearch: {}
            };
        },
        computed: {
            ...mapState('d2admin/user', ['info']),
        },
        methods: {
            formSubmit() {
                this.dataSearch.page = this.currentPage;
                this.dataSearch.pageSize = this.pageSize;
                this.getDataList(this.dataSearch);
            },
            formReset() {
                this.$refs.dataSearch.resetFields();
            },
            handleClick(e) {
                this.$router.push({ path: "/classroom-detail?id=" + e._id });
            },

            handleCurrentChange(currentPage) {
                this.currentPage = currentPage;
                this.dataSearch.page = this.currentPage;
                this.dataSearch.pageSize = this.pageSize;
                this.getDataList(this.dataSearch);
            },
            handlerSizeChange(pageSize) {
                this.pageSize = pageSize;
                this.dataSearch.page = this.currentPage;
                this.dataSearch.pageSize = this.pageSize;
                this.getDataList(this.dataSearch);
            },
            handleNextPage() {
                this.currentPage = this.currentPage + 1;
                this.dataSearch.page = this.currentPage;
                this.dataSearch.pageSize = this.pageSize;
                this.getDataList(this.dataSearch);
            },
            handlePrevClick() {
                this.currentPage = this.currentPage - 1;
                this.dataSearch.page = this.currentPage;
                this.dataSearch.pageSize = this.pageSize;
                this.getDataList(this.dataSearch);
            },
            newPosition() {
                this.$prompt("请输入职位名称", "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                })
                    .then(({value}) => {
                        request({
                            url: "/position/addPosition",
                            method: "post",
                            data: {name: value,school:this.school}
                        }).then(res => {
                            if (res.code == 200) {
                                this.$message({
                                    type: "success",
                                    message: "职位提交成功"
                                });
                                this.getDataList(this.dataSearch);
                            }
                        });
                    })
                    .catch(() => {
                        this.$message({
                            type: "info",
                            message: "取消输入"
                        });
                    });
            },
            editClick(e){
                this.$prompt("请输入职位名称", "职位名称",{
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",

                })
                    .then(({value}) => {
                        request({
                            url: "/position/updatePosition",
                            method: "post",
                            data: {name: value,id:e._id}
                        }).then(res => {
                            if (res.code == 200) {
                                this.$message({
                                    type: "success",
                                    message: "职位修改成功"
                                });
                                this.getDataList(this.dataSearch);
                            }
                        });
                    })
                    .catch(() => {
                        this.$message({
                            type: "info",
                            message: "取消输入"
                        });
                    });
            },
            getDataList(data) {
                var xdata = data 
                xdata.school = this.school
                request({
                    url: "/position/getPositionList",
                    method: "post",
                    data: xdata
                }).then(res => {
                    this.dataList = [];
                    this.dataList = res.data.position;
                    this.total = res.data.total;
                    this.currentPage = res.data.currentPage;
                });
            },
            deleteClick(e) {
                this.$confirm("此操作将删除该职位, 是否继续?", "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                })
                    .then(() => {
                        request({
                            url: "/position/deletePosition",
                            method: "post",
                            data: {id: e._id}
                        }).then(res => {
                            if (res.code == 200) {
                                this.$message({
                                    message: "删除成功"
                                });
                                this.getDataList(this.dataSearch)
                            }
                        });
                    })
                    .catch(() => {
                        this.$message({
                            type: "info",
                            message: "已取消删除"
                        });
                    });
            },
        },
        created() {
            this.dataSearch.page = this.currentPage;
            this.dataSearch.pageSize = this.pageSize;
            this.school = this.info.school[0]._id
            this.getDataList(this.dataSearch);
        }
    };
</script>
